package kr.hkit.powerjava.chapter15.menu;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.GridLayout;

import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class InputEmp extends JPanel implements ActionListener {
	private JLabel lblNum;
	private JButton btnAdd;
	private JPanel panel;
	private JLabel lblSalary;
	private JLabel lblName;
	private JLabel lblTitle;
	private JLabel lblDno;
	private JLabel lblManager;
	private JTextField textNum;
	private JTextField textTitle;
	private JTextField textManager;
	private JTextField textSalary;
	private JTextField textDno;
	private JTextField textName;

	public InputEmp() {

		initialize();
	}
	
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		btnAdd = new JButton("추가");
		btnAdd.addActionListener(this);
		add(btnAdd, BorderLayout.SOUTH);
		
		panel = new JPanel();
		add(panel);
		panel.setLayout(new GridLayout(6, 5, 0, 0));
		
		lblNum = new JLabel("사원번호");
		lblNum.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblNum);
		
		textNum = new JTextField();
		textNum.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(textNum);
		textNum.setColumns(10);
		
		lblName = new JLabel("사원명");
		lblName.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblName);
		
		textName = new JTextField();
		textName.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(textName);
		textName.setColumns(10);
		
		lblTitle = new JLabel("직책");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblTitle);
		
		textTitle = new JTextField();
		textTitle.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(textTitle);
		textTitle.setColumns(10);
		
		lblManager = new JLabel("직속상관");
		lblManager.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblManager);
		
		textManager = new JTextField();
		textManager.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(textManager);
		textManager.setColumns(10);
		
		lblSalary = new JLabel("연봉");
		lblSalary.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblSalary);
		
		textSalary = new JTextField();
		textSalary.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(textSalary);
		textSalary.setColumns(10);
		
		lblDno = new JLabel("부서번호");
		lblDno.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblDno);
		
		textDno = new JTextField();
		textDno.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(textDno);
		textDno.setColumns(10);
	}
	
	public String getLblNewLabel() {
		return lblNum.getText();
	}
	public void setLblNewLabel(String title) {
		this.lblNum.setText(title);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}
	
	private boolean inValidCheck(){
		//공백만 체크
		if (textNum.getText().equals("")) {
			textNum.requestFocus();
			return false;
		}
		if (textName.getText().equals("")) {
			textName.requestFocus();
			return false;
		}
		return true;
	}
	
	private Employee getInfo(){
		if (inValidCheck()) {
			int empNo = Integer.parseInt(textNum.getText().trim());
			String empName = textName.getText().trim();
			String title = textTitle.getText().trim();
			int manager = Integer.parseInt(textManager.getText().trim());
			int salary = Integer.parseInt(textSalary.getText().trim());
			int dno = Integer.parseInt(textDno.getText().trim());
			return new Employee(empNo, empName, title, manager, salary, dno);
		} else {
			return null;
		}
	}
	
	protected void actionPerformedBtnAdd(ActionEvent e) {
		try{
			JOptionPane.showMessageDialog(null, getInfo().toString());
		} catch (NullPointerException e1){
			JOptionPane.showMessageDialog(null, "잘못된 정보");
		}
	}
}
